import org.joda.time.*


/**
 * Creates event points based for the Operating Date passed in
 * the event points are created and returned in Xml or Json format as necessary
 * The Xml or Json data format is based on the requirements for the Simile Timeline project from MIT
 */
class EventPtsUtil
{
    /*
    def getOpDayTimelineItemXml(title, description, start, end, isDuration, isExact, link, imageLink)
    {
        def durationStr = """
            <event
                start="${start}"
                end="${end}"
                isDuration="${isDuration}"
                isExact="${isExact}"
                title="${title}"
                link="${link}"
                image="${imageLink}"
                >
                ${orgName} --> ${bfName} --> ${bpName}
                </event>
        """
    }
    */


    static EventPt getEventPtFromStrings(String domainName, String name, String startDateStr, String startTimeStr, String endDateStr, String endTimeStr, String isDurationStr, String isExactStr)
    {
        EventPt eventPt = new EventPt()
        eventPt.title = name

        if(startTimeStr!=null && startTimeStr.length()>0)
            eventPt.startDate = Utils.parseDateTimeString(startDateStr + " " + startTimeStr)
        else
            eventPt.startDate = Utils.parseDateTimeString(startDateStr + " " + "00:00:00")

        eventPt.isDuration = Utils.parseBoolean(isDurationStr)

        if(eventPt.isDuration)
        {
            if(endTimeStr!=null && endTimeStr.length()>0)
                eventPt.endDate = Utils.parseDateTimeString(endDateStr + " " + endTimeStr)
            else
                eventPt.endDate = Utils.parseDateTimeString(endDateStr + " " + "00:00:00")
        }

        eventPt.link = "../"+domainName+"/" +  name.replace(" ", "_") + ".png"//"http://www.google.com/logo.gif"  
        eventPt.imageLink = "../"+domainName+"/" +  name.replace(" ", "_") + ".png"//"http://www.google.com/logo.gif"  

        return eventPt
    }


    /**
     * get the event point for the operating date
     */
    static def getEventPtForOpDate(name, timelineItem, opDate)
    {
        def eventPt = new EventPt()

        //println ctr + ". " + "Name:" + myBusinessProcess.name
        eventPt.startDate = opDate.plusDays(-timelineItem.startDayOffset).withTime(timelineItem.startDayHour,timelineItem.startDayMins,0,0)
        //println "Start Date:" + utils.getFormattedDateTime(eventPt.startDate)

        if(timelineItem.isDuration)
        {
            eventPt.endDate = opDate.plusDays(-timelineItem.endDayOffset).withTime(timelineItem.endDayHour,timelineItem.endDayMins,0,0)
        }
        else
        {
            eventPt.endDate = eventPt.startDate
        }

        //println "End date:"+utils.getFormattedDateTime(eventPt.endDate)
        //println "Is Duration:"+timelineItem.isDuration
        eventPt.title = name
        eventPt.description = name

        eventPt.isDuration = timelineItem.isDuration
        eventPt.isExact = true

        eventPt.link = "../imagegraphs/" +  name.replace(" ", "_") + ".png"//"http://www.google.com/logo.gif"  
        eventPt.imageLink = "../imagegraphs/" +  name.replace(" ", "_") + ".png"//"http://www.google.com/logo.gif"  

        return eventPt
    }

    
    /**
     * get the bp event pts for op date
     */
    static def getAllBPEventPtsForOpDate(Organization org, DateTime opDate)
    {
        def eventPts = []
        
        def Utils utils = new Utils()

        //println "Operating Date:"+ opDate
        def ctr = 0;
        org.orgServices.each { key, myOrgService -> 
            //println  "\t|----OS:" + myOrgService.name

            myOrgService.businessFunctions.each { bfkey, myBusinessFunction ->
                //println "\t\t|----BF:" + myBusinessFunction.name

                myBusinessFunction.businessProcesses.each { bpkey, myBusinessProcess ->
                    //println "\t\t\t|----BP:" + myBusinessProcess.name
                    //println "\t\t\t\t|-----timeline:" + myBusinessProcess.timelineItem
                    //ctr++
                    //println ctr+ ". " + myBusinessProcess.name + ": " + myBusinessProcess.timelineItem
                    def timelineItem = myBusinessProcess.timelineItem

                    eventPts.add(getEventPtForOpDate(myBusinessProcess.name, timelineItem, opDate))
                }
            }
        }

        return eventPts
    }

    /**
     * returns a timeline of event points for all the sub process event pts
     */
    static def getAllSPEventPtsForOpDate(Organization org, DateTime opDate)
    {
        def eventPts = []
        
        def Utils utils = new Utils()

        //println "Operating Date:"+ opDate
        def ctr = 0;
        org.orgServices.each { key, myOrgService -> 
            //println  "\t|----OS:" + myOrgService.name

            myOrgService.businessFunctions.each { bfkey, myBusinessFunction ->
                //println "\t\t|----BF:" + myBusinessFunction.name

                myBusinessFunction.businessProcesses.each { bpkey, myBusinessProcess ->
                    //println "\t\t\t|----BP:" + myBusinessProcess.name
                    //println "\t\t\t\t|-----timeline:" + myBusinessProcess.timelineItem

                    myBusinessProcess.subProcesses.each { spKey, mySubProcess ->
                        //ctr++
                        //println ctr+ ". " + mySubProcess.name + ": " + mySubProcess.timelineItem
                        def timelineItem = mySubProcess.timelineItem
                        eventPts.add(getEventPtForOpDate(mySubProcess.name, timelineItem, opDate))
                    }
                }
            }
        }

        return eventPts
    }

    
    /**
     * get the event points xml
     */
    static def getEventPtsXml(eventPts)
    {
        // now creating the Business process event Pts
        def timelineXml = "<data>"
        eventPts.each {
            timelineXml = timelineXml << it.getXml()
        }

        timelineXml = timelineXml << "</data>"

        return timelineXml
    }

    /**
     * get the event points Json
     */
    static def getEventPtsJson(eventPts)
    {
        def timelineJson = """
        {
          'wiki-url':"http://simile.mit.edu/shelf/", 
          'wiki-section':"BP Timeline", 
          'dateTimeFormat': 'Gregorian',
          'events': [
        """

        eventPts.each {
            timelineJson = timelineJson << it.getJson() << ","
        }

        timelineJson = timelineJson << "]}\n"
        
        def strBuf = new StringBuffer(timelineJson)
        strBuf.setCharAt(strBuf.lastIndexOf(','), ' ' as char)
        
        //logger.info( "JSON:"+strBuf.toString())
        timelineJson = strBuf.toString()

        return timelineJson
    }
    

    /**
     *
     */
    static def String getDummyTimelineXmlForOpDate(Organization org, String startDateStr, String endDateStr, String timeZone)
    {
        def start = "${startDateStr} 09:00:00 ${timeZone}"
        def end = "${endDateStr} 1:00:00 ${timeZone}"
        def isDuration = "true"
        def isExact = "true"
        def bpName = "bp1"
        def link = "http://www.google.com"
        def imageLink = "http://www.google.com/logo.gif"
        def orgName = "DummyOrg"
        def bfName = "bf1"

        def durationStr = """
            <event
                start="${start}"
                end="${end}"
                isDuration="${isDuration}"
                isExact="${isExact}"
                title="${bpName}"
                link="${link}"
                image="${imageLink}"
                >
                ${orgName} --> ${bfName} --> ${bpName}
                </event>
        """

        def timelineData = """
        <data>
            $durationStr
        </data>
        """

        return timelineData
    }    
}



/*
def eventStr = """
<event
    start="${start}"
    title="${bpName}"
    link="${link}"
    image="${imageLink}"
    >
    ${orgName} --> ${bfName} --> ${bpName}
    </event>
"""
*/
